package com.stoken.web.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.domain.MemberAgentsList;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.TravelApply;
import com.stoken.service.MemberAgentsListService;
import com.stoken.web.StokenUtils;
import com.stoken.web.control.vo.Message;
import com.symbio.commons.Compositor;
import com.symbio.commons.Filtration;
import com.symbio.commons.Page;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.utils.WebUtils;

/**
 * 报单中心管理
 * @author Fred
 *
 */
@Controller
@RequestMapping("/agents")
public class MemberAgentsListControl {
	
	@Autowired
	private MemberAgentsListService memberAgentsListService;
	
	@RequestMapping("/index")
	public String index(){
		return "member/applyagentcenter";		
	}
	
	@RequestMapping("/applyAgent")
	public void applyAgent(HttpServletRequest request,HttpServletResponse response,MemberAgentsList agent){
		MemberBaseInfo  mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		agent.setBaseUsername(mbi);
		agent.setProgressRate(0);
		agent.setApplyTime(new Date());
		memberAgentsListService.save(agent);
		print(response,new Message(1,""));
	}
	
	@RequestMapping("/checkParentAgent")
	public void checkParentAgent(HttpServletRequest request,HttpServletResponse response,String parentAgent){
		boolean boo =  memberAgentsListService.checkParentAgent(parentAgent);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.print(boo);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			out.close();
		}
	}
	
	/**
	 * 输出
	 * @param response
	 * @param message
	 */
	private void print(HttpServletResponse response,Message message){
		try {
			String json = "{\"code\":\""+message.getCode()+"\",\"context\":\""+message.getContext()+"\"}";  
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/backstage/applayAgentPage")
	public String  loadApplayAgent(){
		
		return "/backstage/memberManage/applyAgent";		
	}
	
	@RequestMapping("/backstage/applayAgentData")
	public String  loadApplayAgentData(HttpServletRequest request){		
		
		Page<MemberAgentsList> pageData = new Page<MemberAgentsList>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("applyTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		
		
		pageData = memberAgentsListService.find(pageData);		
		request.setAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		
		request.setAttribute("pagination", page.showPages());
		return "/backstage/memberManage/applyAgentData";		
	}
}
